// JavaScript Document

function $( v ){ //获取元素
	if( typeof v === 'function' ){
		window.onload = v;
	} else if ( typeof v === 'string' ) {
		return document.getElementById(v);
	} else if ( typeof v === 'object' ) {
		return v;
	}
}

function getStyle(obj,attr){ //获取元素样式
	return obj.currentStyle ?  obj.currentStyle[attr] : getComputedStyle(obj)[attr];
}

function addZero(t){  //加0

	return t= t<10 ? "0"+t : ""+t;
}

function doMove(obj,speed,end,attr,endFn){//元素四个方向移动                                
	clearInterval(obj.timer);
	speed = parseInt(getStyle(obj,attr))<end ? speed : -speed;
	obj.timer = setInterval(function (){
		var next = parseInt(getStyle(obj,attr))+speed;
		if(next>=end&&speed>0||next<=end&&speed<0){
			next = end;
		}
		obj.style[attr] = next+"px";
		if(parseInt(getStyle(obj,attr))==end){
			clearInterval(obj.timer);
			if(endFn){endFn();}
			//endFn&&endFn();
		}
	},30)
}


function getPosition(obj,attr){              //块元素到父级的arrt方位的距离
	var iAttr = 0;
	while(obj){
		iAttr += obj["offset"+attr];
		obj=obj.offsetParent;
	}
	return iAttr;
}


function arrIndexOf1(arr,a){                 //求数组元素的定位
	for(var i=0;i<arr.length;i++){
		if(a===arr[i]){
			return i;
		}
	}
}


function getByClass(oParent,sClass,sTagName){
	var aEle = oParent.getElementsByTagName(sTagName);
	var arr = [];
	for(var i=0;i<aEle.length;i++){
		var newArr = aEle[i].className.split(" ");
		for(var j=0;j<newArr.length;j++){
			if(newArr[j]==sClass){
				arr.push(aEle[i]);
				break;
			}
		}
	}
	return arr;
}


function removeClass(obj,sClass){//删除class
	var arr = obj.className.split(" ");
	if(arrIndexOf(arr,sClass)!=-1){
		arr.splice(arrIndexOf(arr,sClass),1);
		obj.className = arr.join(" ");
	}
}
function arrIndexOf(arr,sClass){
	for(var i=0;i<arr.length;i++){
		if(arr[i]==sClass){
			return i;
		}
	}
	return -1;
}
function addClass(obj,sClass){//添加class
	var arr = obj.className.split(" ");
	if(arrIndexOf(arr,sClass)==-1){
		obj.className += " "+sClass;
	}
}
function bind(obj,evname,fn){//事件流
	if(obj.addEventListener){
	obj.addEventListener(evname,fn,false)
	}else{obj.attachEvent('on'+evname,function(){
	fn.call(obj);})
	}
}
function shake(obj,attr,endFn){  //封装抖动函数
//obj谁来抖动，attr是哪个元素值进行改变（横着抖，竖着抖），endFn回调函数（就是抖完之后干嘛）
    // 先获得图片原来left值，在此基础上进行抖动;parseInt()是把300px的单位去掉只留数字
    var pos = parseInt(getStyle(obj,attr));
    var arr = [];//20，-20,18，-18,16，-16，...存放抖动的频率
    var shake = null;//开启定时器,因为动画doMove.js函数中也有定时器，为了避免重复
    var num = 0;//一般只要有数组，那么必定有数字跟着变化；数组的第0个，第1个...

    for(var i =20;i>0;i-=2){
        arr.push(i,-i);
    }
    arr.push(0);
    // alert(arr);//测试一下
    // 有抖动的频率之后，要开启定时器让它进行抖动
    clearInterval(obj.shake);//开启定时器之前要先把 定时器关闭，防止连续多次点击
    obj.shake = setInterval(function(){
        obj.style[attr] = pos + arr[num] + 'px';//原来的位置pos,加上数组中每一个数字
        num++;
        if(num === arr.length){//如果数字等于数组的长度（就是全部抖完了），关闭定时器
            clearInterval(obj.shake);
            // 看是否有回调函数，如果有就执行，没有就不执行
            endFn&&endFn();                     
        }
    },80);
}
/*function drog(obj){ //拖拽事件
obj.onmousedown = function (ev){
	ev = ev || event;
	var disX = ev.clientX - obj.offsetLeft;
	var disY = ev.clientY - obj.offsetTop;
	document.onmousemove = function (ev){
		ev = ev || event;
		var newLeft = ev.clientX - disX;
		var newTop = ev.clientY - disY;

		obj.style.left = newLeft +"px";
		obj.style.top = newTop +"px";
	}
	document.onmouseup = function (){
		document.onmousemove = document.onmouseup = null;
	}
	return false;
}
}*/
function drog1(obj){   //带限制范围的拖拽
obj.onmousedown = function (ev){
	ev = ev || event;
	var disX = ev.clientX - obj.offsetLeft;
	var disY = ev.clientY - obj.offsetTop;
	document.onmousemove = function (ev){
		ev = ev || event;
		var newLeft = ev.clientX - disX;
		var newTop = ev.clientY - disY;
		if(newLeft<=0){//在可视区窗口拖拽，限制范围
			newLeft = 0;
		}else if(newLeft>=oWidth-obj.offsetWidth){
			newLeft = oWidth-obj.offsetWidth;
		}
		if(newTop<=0){
			newTop = 0;
		}else if(newTop>=oHeight-obj.offsetHeight){
			newTop = oHeight-obj.offsetHeight;
		}
		obj.style.left = newLeft +"px";
		obj.style.top = newTop +"px";
	}
	document.onmouseup = function (){
		document.onmousemove = document.onmouseup = null;
	}
	return false;
}
}